package edu.uwm.capstone.diasmart.types;

import java.text.DecimalFormat;
import java.text.ParseException;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.TableRow;
import edu.uwm.capstone.diasmart.R;
import edu.uwm.capstone.diasmart.database.DatabaseHelper;
import edu.uwm.capstone.diasmart.database.Entry;
import edu.uwm.capstone.diasmart.database.SubType;

public class HbA1cType implements EditableEntryType {
	private static final String PATTERN = "##0.0";
	public static final int UNIT_DCCT = 0;
	public static final int UNIT_IFCC = 1;
	private DecimalFormat formatter = new DecimalFormat(PATTERN);

	public static double convertDCCTtoIFCC(double paramDouble) {
		return 10.929D * (paramDouble - 2.15D);
	}

	public static double convertIFCCtoDCCT(double paramDouble) {
		return 2.15D + paramDouble / 10.929D;
	}

	public static int getDisplayedUnit(EntryContext paramEntryContext) {
		String str = paramEntryContext.getContext().getString(
				R.string.hba1c_unit_preference_key);
		if ("dcct".equals(paramEntryContext.getPreferences().getString(
				str,
				paramEntryContext.getContext().getString(
						R.string.hba1c_unit_preference_dcct))))
			return 0;
		return 1;
	}

	@Override
	public String getEditableValue(EntryContext paramEntryContext,
			double paramDouble) {
		return getFormattedValue(paramEntryContext, paramDouble).replace(
				this.formatter.getDecimalFormatSymbols().getDecimalSeparator(),
				'.');

	}

	@Override
	public String getFormattedValue(EntryContext paramEntryContext,
			double paramDouble) {
		return this.formatter.format(paramDouble);
	}

	@Override
	public String getMeasurementUnit(EntryContext paramEntryContext) {
		if (getDisplayedUnit(paramEntryContext) == 0)
			return paramEntryContext.getContext().getString(
					R.string.percent_unit);
		return paramEntryContext.getContext().getString(R.string.mmolmol);
	}

	@Override
	public String getMeasurementUnit(EntryContext paramEntryContext,
			SubType paramSubType) {
		if ((paramSubType == null) || (paramSubType.getUnit() == null))
			return getMeasurementUnit(paramEntryContext);
		return paramSubType.getUnit();
	}

	@Override
	public int getType() {
		return 7;
	}

	@Override
	public double toActualValue(EntryContext paramEntryContext, long paramLong) {
		double d = paramLong / 1000.0D;
		if (getDisplayedUnit(paramEntryContext) == 1)
			d = convertDCCTtoIFCC(d);
		return d;
	}

	@Override
	public long toStoredValue(EntryContext paramEntryContext, double paramDouble) {
		double d = paramDouble;
		if (getDisplayedUnit(paramEntryContext) == 1)
			d = convertIFCCtoDCCT(d);
		return (long) (d * 1000.0D);
	}

	@Override
	public double toValue(EntryContext paramEntryContext, String paramString) {
		String str = paramString.replace('.', this.formatter
				.getDecimalFormatSymbols().getDecimalSeparator());
		try {
			double d = this.formatter.parse(str).doubleValue();
			return d;
		} catch (ParseException localParseException) {
		}
		return Double.parseDouble(str);
	}

	@Override
	public String getKeyboardType() {
		return "preference";
	}

	@Override
	public TableRow[] getUserInterface(Activity paramActivity) {
		return EditableEntryTypeHelper.getStandardUserInterface(paramActivity,
				this, R.id.edtHbA1cValue, R.id.sprHbA1cSubType);

	}

	@Override
	public boolean handleOnActivityResult(Activity paramActivity,
			int paramInt1, int paramInt2, Intent paramIntent) {
		return EditableEntryTypeHelper.handleOnActivityResult(paramActivity,
				getType(), R.id.sprHbA1cSubType, paramInt1, paramInt2,
				paramIntent);

	}

	@Override
	public long save(DatabaseHelper paramDatabaseHelper, Entry paramEntry,
			View paramView) {
		return EditableEntryTypeHelper
				.saveStandard(paramDatabaseHelper, this, paramEntry, paramView,
						R.id.edtHbA1cValue, R.id.sprHbA1cSubType);

	}

	@Override
	public int validate(View paramView) {
		return EditableEntryTypeHelper.validateStandard(this, paramView,
				R.id.edtHbA1cValue, R.id.sprHbA1cSubType);
	}

	@Override
	public int validate(EntryContext paramEntryContext, String paramString) {
		return EditableEntryTypeHelper.validateStandard(
				paramEntryContext.getContext(), paramString);
	}

	public String toString() {
		return "HbA1c";
	}

}
